package com.jian.java.one;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @author Jiange
 * @create 2021-09-21-9:37
 */
public class Consumer {
    public static final String QUEUE_NAME = "hello";

    public static void main(String[] args) throws IOException, TimeoutException {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("123456");
        connectionFactory.setHost("127.0.0.1");

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        DeliverCallback callback = (ConsumerTag,message)->{
            System.out.println(message.getBody());
        };
        CancelCallback cancelCallback = consumerTag -> {
            System.out.println("消费被中断");
        };

        //1.队列名  2.消费成功是否自动应答 true自动应答  3.成功消费的回调   4.消费者取消消费的回调
        channel.basicConsume(QUEUE_NAME,true,callback,cancelCallback);
    }
}
